package com.code.google.wormon;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import java.lang.Math;


import com.code.google.wormon.base.Game;
import com.code.google.wormon.base.GameObject;

public class WormonGangster implements GameObject {

	private Rectangle shape;
	private Image image;
	private Game game;
	private boolean isGuy = false;
	final int periGangster = 100;

	public WormonGangster(Game game) {
		super();
		this.game = game;
		image = new ImageIcon(getClass().getClassLoader().getResource("com/code/google/wormon/resources/gangworm.png")).getImage();
		shape = new Rectangle(300, 150, image.getWidth(null), image.getHeight(null));
	}

	@Override
	public void collided(GameObject other) {
	}

	@Override
	public void react(KeyEvent keyEvent) {
		/**
		 * no key listener for this class. Its a npc, dar!
		 */
	}

	@Override
	public void draw(Graphics2D g2d) {
		g2d.drawImage(image, shape.x, shape.y, null);
	}

	@Override
	public Rectangle getBounds() {
		return shape;
	}

	@Override
	public void doLogic() {
		GameObject guy = game.getGuy();
		if (guy != null) {
			if (shape.x > guy.getPosX()-periGangster &&
				shape.x < guy.getPosX()+periGangster &&
				shape.y < guy.getPosY()+periGangster &&
				shape.y > guy.getPosY()-periGangster) {
				WormonSoundFx.ALERT.playOnce();
			} else {
				WormonSoundFx.ALERT.stop();
			}
		}
		Random randomSide = new Random();
		Random random = new Random();
		if (randomSide.nextInt(4) == 0) {
			shape.y = shape.y - random.nextInt(4);
		}
		;
		if (randomSide.nextInt(4) == 1) {
			shape.y = shape.y + random.nextInt(4);
		}
		;
		if (randomSide.nextInt(4) == 2) {
			shape.x = shape.x - random.nextInt(4);
		}
		;
		if (randomSide.nextInt(4) == 3) {
			shape.x = shape.x + random.nextInt(4);
		}
		;

	}

	@Override	
	public int getPosX(){
		return shape.x;
	}
	
	@Override
	public int getPosY(){
		return shape.y;
	}

}
